---
title: "SI Replication for ``How Information about Foreign Aid Affect Public Spending Decisions: Evidence from a Field Experiment in Malawi''"
author: "Ryan Jablonski"
date: "16 July 2020"
output:
  pdf_document:
    toc: yes
  word_document:
    toc: yes
  html_document:
    theme: united
    toc: yes
header-includes: \usepackage{float, makecell, longtable, placeins} 
editor_options:
  chunk_output_type: console
---

\section{Replication Notes}
This file creates a replication file containing the supplementary tables and figures from Seim, Brigitte, Ryan Jablonski and Johan Ahlback. "How Information about Foreign Aid Affect Public Spending Decisions: Evidence from a Field Experiment in Malawi" in the \textit{Journal of Development Economics}. 

This code was originally run on RStudio Version 1.2.5033 with R 3.5.0. All data has been anonymized to protect respondents. 

For questions about this replication contact Ryan Jablonski at r.s.jablonski@lse.ac.uk.



```{r setup, include=FALSE, echo=FALSE}
rm(list=ls(all=TRUE))
set.seed(34685) #from original sampling file

rootdir="C:\\Users\\Ryan.LSE063315\\Dropbox\\Foreign Aid in Malawi\\Round 2 Experiment\\Analysis\\JDE Replication\\"

knitr::opts_knit$set(root.dir = rootdir)
setwd(rootdir)

library(ggplot2)
library(units)
library(survival)
library(lfe) 
library(lme4)
library(foreign)
library(broom)
library(dotwhisker)
library(dplyr)
library(stargazer)
library(MASS)
library(wesanderson)
library(corrplot)
library(dplyr)
library(xtable)
library(randomizr)
library(compiler)
library(mlogit)


mp.survey=read.csv("./data/mp_withcovariates.csv", stringsAsFactors = FALSE)
c.survey=read.csv("./data/c_withcovariates.csv", stringsAsFactors = FALSE)

#check anonymity here!
mp.survey.all=read.csv("./data/mp_withcovariates_withattritted.csv", stringsAsFactors = FALSE)
c.survey.all=read.csv("./data/c_withcovariates_withattritted.csv", stringsAsFactors = FALSE)


all.surveys=read.csv("./data/all_withcovariates.csv", stringsAsFactors = FALSE)
labels=read.csv("./data/labels.csv",stringsAsFactors=FALSE)

schools.lc=read.csv(".\\data\\lc_schools_all.csv", stringsAsFactors = FALSE)
schools.mp=read.csv(".\\data\\mp_schools_all.csv", stringsAsFactors = FALSE)



#these are labels for our variables to keep things pretty. We also use this to define the scope of control variables.
labels=subset(labels, !is.na(varname))
labels.all=labels
labels=labels[labels$include==1,]
labels = labels[order(labels$label),]
labels=labels[labels$alt.specific==1,]


#Create a vector of control variables to use in the analysis
controls = ""
for(i in c(1:nrow(labels))){
  controls = paste(controls, labels$varname[i], sep=" + ")
}

controls=paste(controls, "winner_percent_imp", sep=" + ")
controls=paste(controls, "any_children_attend_school", sep=" + ")
controls=gsub("school_need_index_constituency \\+ ", "", controls)
controls=gsub(" \\+  school_need_index_ward","", controls)



labelme=function(varname){
  this.label=""
  if(!grepl(":",varname)){
    this.label=labels.all[labels.all$varname==varname,]$label
  } else {
    varnames=strsplit(varname, ":")[[1]]
    this.label=labels.all[labels.all$varname==varnames[1],]$label
    for(i in 2:length(varnames)){
      this.label=paste(this.label, labels.all[labels.all$varname==varnames[i],]$label, sep="*")
    }
  }
  return(this.label)
}
  

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}



get_marginal_effect=function(this.xvar, this.pvar="donor_knowledge", this.model, vmatrix=NULL, p1=0){

  t1=1
  t0=0  
  beta <- coef(this.model)
  
  beta[!is.finite(beta)] <- 0
  
  covvar <- vcov(this.model)
  if(!is.null(vmatrix)){
    covvar=vmatrix
  }
  
  xvars=c(log(2), log(3), log(4), log(5))
  
  beta.sim <- mvrnorm(10000, beta,covvar)
  varnames=colnames(beta.sim)
  
  
  x0= matrix(0, ncol = length(varnames), nrow = length(xvars))
  x1= matrix(0, ncol = length(varnames), nrow = length(xvars))
  i=0
  for(this.name in varnames){
    i=i+1
    if(grepl(paste("^", this.xvar, "$", sep=""), this.name)){
      x0[,i]=xvars
      x1[,i]=xvars
      next
    }
    
    if(grepl(paste("^information_aid:", this.xvar, "$", sep=""), this.name)){
      x0[,i]=xvars*t0
      x1[,i]=xvars*t1
      next
    }
    
    if(this.name==paste("",this.xvar, ":", this.pvar, "", sep="")){
        
      x0[,i]=xvars*p1
      x1[,i]=xvars*p1
      next
    }
    
    if(this.name==paste("information_aid:",this.xvar,":", this.pvar, "", sep="")){
        
      x0[,i]=xvars*p1*t0
      x1[,i]=xvars*p1*t1
      next
    }
   
  }
  
  xb1 <- x1 %*% t(beta.sim)
  xb0 <- x0 %*% t(beta.sim)
  p1=xb1
  p0=xb0
  p1=exp(xb1)/(1+exp(xb1))
  p0=exp(xb0)/(1+exp(xb0))
  
  e=p1-p0
 
  predicted.e <- apply(e,1,quantile, probs =  c(.025,.5,.975))
  predictions=rbind(predicted.e, apply(e,1,mean), apply(e,1,sd))

  return(predictions)
}

```

\section{Additional Treatment Effect Estimates}
\subsection{Table A1 multiple comparison}

```{r multiplecomparison, echo=FALSE, warning=FALSE, results='asis', eval=T}


##all surveys

hd1a = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
hd1b = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
hd2 = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories*donor_frequency + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
hd4 = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories*winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
hd5 = clogit(as.formula(paste("incumbentselected ~ information_aid*number_aid_categories*school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)

results=data.frame(Hypothesis="HD1/3a. Politicians will be more likely to allocate to schools that have already benefitted from more past aid projects.", p_value_unadjusted=tidy(hd1a)$p.value[3])

results=rbind(results, data.frame(Hypothesis="HD1/3b. Politicians will be more likely to allocate to schools where donors have provided more categories of goods.", p_value_unadjusted=tidy(hd1b)$p.value[3]))
results=rbind(results, data.frame(Hypothesis="HD.2 Validation will be more likely when politicians interact frequently with donors.", p_value_unadjusted=tidy(hd2)$p.value[7]))
results=rbind(results, data.frame(Hypothesis="HD.4 Crowding out will be more likely in areas where the politician did not receive a high proportion of votes.", p_value_unadjusted=tidy(hd4)$p.value[7]))
results=rbind(results, data.frame(Hypothesis="HD.5 Crowding out will be more likely in areas where schools are less needy.", p_value_unadjusted=tidy(hd5)$p.value[7]))

results=results[order(results$p_value_unadjusted),]
results$unadjusted=results$p_value_unadjusted
results$BH=c(round(p.adjust(results$p_value_unadjusted[1:2], method="BH"),4), NA, NA, NA)
results$bonferroni=c(round(p.adjust(results$p_value_unadjusted[1:2], method="bonferroni"),4),NA,NA,NA)
results$BH2=round(p.adjust(results$p_value_unadjusted, method="BH"),4)
results$bonferroni2=round(p.adjust(results$p_value_unadjusted, method="bonferroni"),4)
results$p_value_unadjusted=NULL


print(xtable(results,  digits=4, label="tab_multiplecomparison_all_subsets", caption="Multiple Comparison Adjustment", align=c("p{0.01\\textwidth}|",
               "p{0.3\\textwidth}|",
               "p{0.1\\textwidth}|",
               "p{0.1\\textwidth}|",
               "p{0.1\\textwidth}|",
               "p{0.1\\textwidth}|",
               "p{0.1\\textwidth}|"
               )), include.rownames=FALSE,  caption.placement = "top", hline.after=c(-1, 0))



```
\FloatBarrier

\subsection{Table A2 multiple comparison}


```{r, echo=FALSE, warning=FALSE, results='asis', eval=T}


ha1 = clogit(as.formula(paste("incumbentselected ~ z_school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
ha2 = clogit(as.formula(paste("incumbentselected ~ z_winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)  
ha4 = clogit(as.formula(paste("incumbentselected ~ any_children_attend_school + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)  
ha5 = clogit(as.formula(paste("incumbentselected ~ z_pop_per_hectacre + strata(map_id) + cluster(resp_id)")),  method="efron", all.surveys)  

results=data.frame(Hypothesis="HA.1. Politicians will allocate more aid to schools with high need than with low need.", p_value_unadjusted=tidy(ha1)$p.value)
results=rbind(results, data.frame(Hypothesis="HA.2. Politicians will allocate more aid to schools located in areas with higher support for the politician in the last election.", p_value_unadjusted=tidy(ha2)$p.value))
results=rbind(results, data.frame(Hypothesis="HA.4. Politicians will allocate more aid to schools where their family members attend.", p_value_unadjusted=tidy(ha4)$p.value))
results=rbind(results, data.frame(Hypothesis="HA.5. Politicians will allocate more aid to schools located in densely populated areas.", p_value_unadjusted=tidy(ha5)$p.value))

results$unadjusted=results$p_value_unadjusted

results=results[order(results$p_value_unadjusted),]
results$BH=round(p.adjust(results$p_value_unadjusted, method="BH"),4)
results$bonferroni=round(p.adjust(results$p_value_unadjusted, method="bonferroni"),4)
results$p_value_unadjusted=NULL

results.ha=results


print(xtable(results.ha,  digits=4, label="tab_multiplecomparison_unconditional", caption="Multiple Comparison Adjustment, Unconditional Effects", align=c(
               "p{0.01\\textwidth}|",
               "p{0.3\\textwidth}|",
               "p{0.2\\textwidth}|",
               "p{0.2\\textwidth}|",
               "p{0.2\\textwidth}|"
              
               )), include.rownames=FALSE,  caption.placement = "top", hline.after=c(-1, 0))


```


\FloatBarrier

\subsection{Table A3 global multiple comparison}
Due to the large number of simulations, this code will take a very long time to run. The code is currently set to 100 simulations, though results will vary at this N. The published results are based on 1,000 simulations. 

```{r, echo=FALSE, warning=FALSE, results='asis', eval=T}
n.sims=100


all.surveys$s_information_votes=NA
all.surveys$s_information_need=NA
all.surveys$s_information_aid=NA

all.surveys.uninformed=all.surveys[all.surveys$donor_knowledge<=median(all.surveys$donor_knowledge),]

results=data.frame()

#get number of maps
n.maps=length(unique(all.surveys$map_id))

#create indicators for all possible treatment arm combinations for sampling
maps.treatment = data.frame(id=c("C", "D", "E", "G", "I", "K", "L", "N"), 
                            s_information_aid =c(1, 0, 0, 1, 1, 1, 0, 0), s_information_need=c(0, 1, 0, 1, 0, 1, 1, 0), s_information_votes=c(0, 0, 1, 0, 1, 1, 1, 0))


#setup hypothesis tests and get unadjusted p values
this.df=all.surveys
h1 = clogit(as.formula(paste("incumbentselected ~ past_aid_project + information_aid:past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
h2 = clogit(as.formula(paste("incumbentselected ~ number_aid_categories + information_aid:number_aid_categories  + strata(map_id) + cluster(resp_id)")), method="efron", this.df)


results=data.frame(Hypothesis="H1", p_value_unadjusted=tidy(h1)$p.value[2])
results=rbind(results, data.frame(Hypothesis="H2", p_value_unadjusted=tidy(h2)$p.value[2]))
p_obs=results$p_value_unadjusted


#setup hypothesis tests and get unadjusted p values for uninformed sample
this.df=all.surveys.uninformed
h1 = clogit(as.formula(paste("incumbentselected ~ past_aid_project + information_aid:past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
h2 = clogit(as.formula(paste("incumbentselected ~ number_aid_categories + information_aid:number_aid_categories  + strata(map_id) + cluster(resp_id)")), method="efron", this.df)

results=data.frame(Hypothesis="H1", p_value_unadjusted=tidy(h1)$p.value[2])
results=rbind(results, data.frame(Hypothesis="H2", p_value_unadjusted=tidy(h2)$p.value[2]))
p_obs_uninformed=results$p_value_unadjusted




do_test12 <- function(this.df){
  
  h = clogit(as.formula(paste("incumbentselected ~ past_aid_project + s_information_aid:past_aid_project + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=c(tidy(h)$p.value[2])
  
  h = clogit(as.formula(paste("incumbentselected ~ number_aid_categories + s_information_aid:number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  return(this.results)
  
}


do_test34 <- function(this.df){
 
  h = clogit(as.formula(paste("incumbentselected ~ past_aid_project + s_information_aid:past_aid_project + donor_knowledge:past_aid_project + past_aid_project:donor_knowledge:s_information_aid + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=c(tidy(h)$p.value[2])
    
  h = clogit(as.formula(paste("incumbentselected ~ number_aid_categories + s_information_aid:number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  h = clogit(as.formula(paste("incumbentselected ~ school_need_index + s_information_need:school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  h = clogit(as.formula(paste("incumbentselected ~ winner_percent + s_information_votes:winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  return(this.results)
 
}


do_test56 <- function(this.df){
  #all direct effects of treatment (regardless of whether it is the main hypothesis)
  
  #aid informaiton
  h = clogit(as.formula(paste("incumbentselected ~ past_aid_project + s_information_aid:past_aid_project + donor_knowledge:past_aid_project + past_aid_project:donor_knowledge:s_information_aid + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=c(tidy(h)$p.value[2])
  
  h = clogit(as.formula(paste("incumbentselected ~ number_aid_categories + s_information_aid:number_aid_categories + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  #need information
  h = clogit(as.formula(paste("incumbentselected ~ school_need_index + s_information_need:school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  h = clogit(as.formula(paste("incumbentselected ~ winner_percent + s_information_need:winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  h = clogit(as.formula(paste("incumbentselected ~ family_children_attend_school + s_information_need:family_children_attend_school + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  #voter information
  h = clogit(as.formula(paste("incumbentselected ~ winner_percent + s_information_votes:winner_percent + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  h = clogit(as.formula(paste("incumbentselected ~ school_need_index + s_information_votes:school_need_index + strata(map_id) + cluster(resp_id)")), method="efron", this.df)
  this.results=append(this.results,c(tidy(h)$p.value[2]))
  
  
  return(this.results)
  
  
}





#this function resamples the treatments under the null and re-estimates p-values using the function modelfun. We repeat this function n.sim times to get our distribution of p-values under the null.
permute_treat=function(modelfun, mysurvey){
  
  resp_ids=unique(mysurvey$resp_id)
  survey.collapse=mysurvey[!duplicated(mysurvey$map_id),c("map_id", "resp_id", "s_information_votes", "s_information_need", "s_information_aid")]


  for(resp_id in resp_ids)
  {
   
    this.survey=survey.collapse[survey.collapse$resp_id==resp_id,]
    s_treat=sample_n(maps.treatment, nrow(this.survey), replace=FALSE)
    this.survey$s_information_aid=s_treat$s_information_aid
    this.survey$s_information_votes=s_treat$s_information_votes
    this.survey$s_information_need=s_treat$s_information_need
    survey.collapse[survey.collapse$resp_id==resp_id,]=this.survey
    
  }
  
  mysurvey$s_information_aid=survey.collapse[match(mysurvey$map_id, survey.collapse$map_id),"s_information_aid"]
  mysurvey$s_information_votes=survey.collapse[match(mysurvey$map_id, survey.collapse$map_id),"s_information_votes"]
  mysurvey$s_information_need=survey.collapse[match(mysurvey$map_id, survey.collapse$map_id),"s_information_need"]
  
  
  ps_sim=modelfun(mysurvey)
  
  return(ps_sim)
}


#compile the functions for slight optimization gain
permute_treat_cmp <- cmpfun(permute_treat)



threshold_finder<- function(threshold, many_ps){
  mean(apply(many_ps, 2, x <- function(x) sum(x <= threshold) > 0 ))
}

ptm <- proc.time()


#create a matrix of p-values under the null for each class of hypothesis tests
#this chunk will take a long time! 
many_ps12=sapply(1:n.sims, function(x) permute_treat_cmp(do_test12, all.surveys))
many_ps34=sapply(1:n.sims, function(x) permute_treat_cmp(do_test34, all.surveys))
many_ps56=sapply(1:n.sims, function(x) permute_treat_cmp(do_test56, all.surveys))

many_ps12_uninformed=sapply(1:n.sims, function(x) permute_treat_cmp(do_test12, all.surveys.uninformed))
many_ps34_uninformed=sapply(1:n.sims, function(x) permute_treat_cmp(do_test34, all.surveys.uninformed))
many_ps56_uninformed=sapply(1:n.sims, function(x) permute_treat_cmp(do_test56, all.surveys.uninformed))

print(proc.time() - ptm)



# Compare observed to p-value to distribution under the null to obtain the Type I error rate for each class of hypotheses
type_I_rate_hyp12 <- sapply(c(p_obs[1], p_obs[2]), threshold_finder, many_ps=many_ps12)
type_I_rate_hyp34 <- sapply(c(p_obs[1], p_obs[2]), threshold_finder, many_ps=many_ps34)
type_I_rate_hyp56 <- sapply(c(p_obs[1], p_obs[2]), threshold_finder, many_ps=many_ps56)

type_I_rate_hyp12_uninformed <- sapply(c(p_obs_uninformed[1], p_obs_uninformed[2]), threshold_finder, many_ps=many_ps12_uninformed)
type_I_rate_hyp34_uninformed <- sapply(c(p_obs_uninformed[1], p_obs_uninformed[2]), threshold_finder, many_ps=many_ps34_uninformed)
type_I_rate_hyp56_uninformed <- sapply(c(p_obs_uninformed[1], p_obs_uninformed[2]), threshold_finder, many_ps=many_ps56_uninformed)



df=data.frame(Main_Sample=c(type_I_rate_hyp12, type_I_rate_hyp34, type_I_rate_hyp56), Uninformed_Sample=c(type_I_rate_hyp12_uninformed, type_I_rate_hyp34_uninformed, type_I_rate_hyp56_uninformed))

print(xtable(df,  digits=4, label="tab_global_multiplecomparison", caption="Global Multiple Comparison Adjustment"), include.rownames=T,  caption.placement = "top")



```
\FloatBarrier

\subsection{Table A4 compliance measures}
```{r compliance, echo=F, warning=FALSE, results='asis'}

h.compliance1 = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*notunderstandmap",controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
#summary(h.compliance1)

h.compliance1b = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*notunderstandmap + information_aid*past_aid_project*mp + ",controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
#summary(h.compliance1b)

h.compliance2 = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*otherschool",controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
#summary(h.compliance2)

h.compliance3 = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*wrongschool",controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
#summary(h.compliance3)

h.compliance4 = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*othermaterials",controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
#summary(h.compliance4)

stargazer(h.compliance1, h.compliance2, h.compliance3, h.compliance4, type = "latex", 
          out=paste("./Output/tab_compliance.tex", sep=""), 
          column.labels = c("1","2", "3",  "4"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="Treatment Effects by Compliance",
          order=c("^information_aid:past_aid_project:notunderstandmap$","^information_aid:past_aid_project:otherschool$", "^information_aid:past_aid_project:wrongschool$", "^information_aid:past_aid_project:othermaterials$", "^information_aid:past_aid_project$", "^past_aid_project:notunderstandmap$", "^past_aid_project:otherschool$", "^past_aid_project:wrongschool$", "^past_aid_project:othermaterials$", "^past_aid_project$"),
          keep=c("^information_aid:past_aid_project:notunderstandmap$","^information_aid:past_aid_project:otherschool$", "^information_aid:past_aid_project:wrongschool$", "^information_aid:past_aid_project:othermaterials$", "^information_aid:past_aid_project$", "^past_aid_project:notunderstandmap$", "^past_aid_project:otherschool$", "^past_aid_project:wrongschool$", "^past_aid_project:othermaterials$", "^past_aid_project$"),
          covariate.labels = c(paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, "Misunderstood Maps (Q1.22)", sep="*\n"), 
                               paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, "Requested Other School (Q1.71)", sep="*\n"), 
                               paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label , "Disputed Map (Q1.72)", sep="*\n"), 
                               paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label , "Requested Other Goods (Q1.73)", sep="*\n"), 
                               paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, sep="*\n"), 
                               paste(labels.all[labels.all$varname=="past_aid_project",]$label, "Misunderstood Maps (Q1.22)", sep="*\n"),   
                               paste(labels.all[labels.all$varname=="past_aid_project",]$label, "Requested Other School (Q1.71)", sep="*\n"),   
                               paste(labels.all[labels.all$varname=="past_aid_project",]$label, "Disputed Map (Q1.72)", sep="*\n"),   
                               paste(labels.all[labels.all$varname=="past_aid_project",]$label, "Requested Other Goods (Q1.73)", sep="*\n"),   
                               
                               labels.all[labels.all$varname=="past_aid_project",]$label),
          omit="*",
          no.space=TRUE,
          label=paste("tab_compliance", sep=""),
          table.placement = "H"
          
)
```

\FloatBarrier



\subsection{Table A5-A6 mixed logit}
```{r mixed logit, echo=F, warning=FALSE, results='asis', eval=F}



cluster_mlogit=function(fm, cluster, vc=FALSE){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- length(coefficients(fm))
  dfc <- (M/(M-1))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat.=crossprod(uj)/N)
  if(vc==FALSE){return(coeftest(fm, vcovCL))}
  if(vc==TRUE){return(vcovCL)}
}



n.sims=1000

c.survey=c.survey[!is.na(c.survey$information_aid),]
c.survey=c.survey[!is.na(c.survey$past_aid_project),]
c.survey=c.survey[!is.na(c.survey$incumbentselected),]
c.survey=c.survey[!is.na(c.survey$number_aid_categories),]

mp.survey=mp.survey[!is.na(mp.survey$information_aid),]
mp.survey=mp.survey[!is.na(mp.survey$past_aid_project),]
mp.survey=mp.survey[!is.na(mp.survey$incumbentselected),]
mp.survey=mp.survey[!is.na(mp.survey$number_aid_categories),]

all.surveys=all.surveys[!is.na(all.surveys$information_aid),]
all.surveys=all.surveys[!is.na(all.surveys$past_aid_project),]
all.surveys=all.surveys[!is.na(all.surveys$incumbentselected),]
all.surveys=all.surveys[!is.na(all.surveys$number_aid_categories),]



#some cleanup so that we have all the ids and objects we need for mlogit
for(dfname in c("c.survey", "mp.survey", "all.surveys")){
  decisions=eval(parse(text=dfname))
  decisions$count=1
  t=aggregate(decisions$count, list(decisions$map_id), sum) 
  maps= t$Group.1
  
  decisions$school_order_id=NA
  for(m in maps){
    
    if(nrow(decisions[decisions$map_id==m,])==3){
      decisions[decisions$map_id==m,]$school_order_id=c(1,2,3)
    } else { print(paste("we have a problem, houston", m))}
    
  }
  
   decisions$altvar = paste("s", decisions$school_order_id, sep="")
  
  decisions <- decisions[order(decisions$resp_id, decisions$map_id, decisions$altvar),]
  
  mdecisions=mlogit.data(decisions, shape="long", choice="incumbentselected", alt.var = "altvar", chid.var = "map_id", id="resp_id")
  clusterdata=mdecisions[!duplicated(mdecisions$map_id),]
  mdecisions$information_aidXpast_aid_project= mdecisions$information_aid*mdecisions$past_aid_project
  mdecisions$information_aidXnumber_aid_categories = mdecisions$information_aid*mdecisions$number_aid_categories
  
  assign(dfname, decisions)
  assign(paste("m_", dfname, sep=""), mdecisions)
  assign(paste("cdata_", dfname, sep=""), clusterdata)
  
}



m1 = mlogit(as.formula(paste("incumbentselected ~ information_aidXpast_aid_project + past_aid_project")), m_c.survey, rpar=c(past_aid_project='n', information_aidXpast_aid_project='n'), R=n.sims, halton=NA, print.level=0, panel=TRUE, correlation=FALSE)    
m1.c=cluster_mlogit(m1, cdata_c.survey$resp_id)
m1.vcov=cluster_mlogit(m1, cdata_c.survey$resp_id, TRUE)

m2 = mlogit(as.formula(paste("incumbentselected ~ information_aidXpast_aid_project + past_aid_project")), m_mp.survey, rpar=c(past_aid_project='n', information_aidXpast_aid_project='n'), R=n.sims, halton=NA, print.level=0, panel=TRUE, correlation=FALSE)    
m2.c=cluster_mlogit(m2, cdata_mp.survey$resp_id)
m2.vcov=cluster_mlogit(m2, cdata_mp.survey$resp_id, TRUE)

m3 = mlogit(as.formula(paste("incumbentselected ~ information_aidXpast_aid_project + past_aid_project")), m_all.surveys, rpar=c(past_aid_project='n', information_aidXpast_aid_project='n'), R=n.sims, halton=NA, print.level=0, panel=TRUE, correlation=FALSE)    
m3.c=cluster_mlogit(m3, cdata_all.surveys$resp_id)
m3.vcov=cluster_mlogit(m3, cdata_all.surveys$resp_id, TRUE)
########

stargazer(m1,m2,m3, type = "latex", 
          out=paste("./Output/tab_information_aid_mlogit.tex", sep=""), 
          column.labels = c("LCs","MPs", "All Respondents"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="Mixed Logit Estimates of the Effect of Information about Past Aid",
          order=c("^information_aidXpast_aid_project$", "^past_aid_project$"),
          keep=c("^information_aidXpast_aid_project$", "^past_aid_project$"),
          covariate.labels = c(paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, sep="*\n"), labels.all[labels.all$varname=="past_aid_project",]$label),
          omit="*",
          no.space=TRUE,
          label=paste("tab_information_aid_mlogit", sep=""),
          table.placement = "H",
          se=list(m1.c[,2], m2.c[,2], m3.c[,2])
)






m1 = mlogit(as.formula(paste("incumbentselected ~ information_aidXnumber_aid_categories + number_aid_categories")), m_c.survey, rpar=c(number_aid_categories='n', information_aidXnumber_aid_categories='n'), R=n.sims, halton=NA, print.level=0, panel=TRUE, correlation=FALSE)    
m1.c=cluster_mlogit(m1, cdata_c.survey$resp_id)
m1.vcov=cluster_mlogit(m1, cdata_c.survey$resp_id, TRUE)

m2 = mlogit(as.formula(paste("incumbentselected ~ information_aidXnumber_aid_categories + number_aid_categories")), m_mp.survey, rpar=c(number_aid_categories='n', information_aidXnumber_aid_categories='n'), R=n.sims, halton=NA, print.level=0, panel=TRUE, correlation=FALSE)    
m2.c=cluster_mlogit(m2, cdata_mp.survey$resp_id)
m2.vcov=cluster_mlogit(m2, cdata_mp.survey$resp_id, TRUE)

m3 = mlogit(as.formula(paste("incumbentselected ~ information_aidXnumber_aid_categories + number_aid_categories")), m_all.surveys, rpar=c(number_aid_categories='n', information_aidXnumber_aid_categories='n'), R=n.sims, halton=NA, print.level=0, panel=TRUE, correlation=FALSE)    
m3.c=cluster_mlogit(m3, cdata_all.surveys$resp_id)
m3.vcov=cluster_mlogit(m3, cdata_all.surveys$resp_id, TRUE)





stargazer(m1, m2, m3, type = "latex", 
          out=paste("./Output/tab_information_categories_mlogit.tex", sep=""), 
          column.labels = c("LCs","MPs", "All Respondents"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="Mixed Logit Estimates of the Effect of Information about Aid Good Types",
          order=c("^information_aidXnumber_aid_categories$", "^number_aid_categories$"),
          keep=c("^information_aidXnumber_aid_categories$", "^number_aid_categories$"),
          covariate.labels = c(paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="number_aid_categories",]$label, sep="*\n"), labels.all[labels.all$varname=="number_aid_categories",]$label),
          omit="*",
          no.space=TRUE,
          label=paste("tab_information_categories_mlogit", sep=""),
          table.placement = "H",
          se=list(m1.c[,2], m2.c[,2], m3.c[,2])

          
)


```

\FloatBarrier

\subsection{Table A7 interaction on good type}
```{r Interaction on Good Type, message=FALSE, warning=FALSE, results='asis', echo=FALSE}

h.bags = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*good_type_bags + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
h.dictionary = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*good_type_dictionary + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
h.lamps = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*good_type_lamps + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
h.all = clogit(as.formula(paste("incumbentselected ~ information_aid*past_aid_project*good_type_bags*good_type_dictionary*good_type_lamps + strata(map_id) + cluster(resp_id)")), method="efron", all.surveys)
#summary(h.all)

stargazer(h.bags, h.dictionary, h.lamps, h.all, type = "latex", 
          out=paste("./Output/tab_good_heterogeneity.tex", sep=""), 
          column.labels = c("School Bags","Dictionaries", "Lamps",  "All"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="Heterogenous Effects by Good Type",
          order=c("^information_aid:past_aid_project:good_type_bags$","^information_aid:past_aid_project:good_type_dictionary$", "^information_aid:past_aid_project:good_type_lamps$", "^information_aid:past_aid_project$", "^past_aid_project:good_type_bags$", "^past_aid_project:good_type_dictionary$", "^past_aid_project:good_type_lamps$", "^past_aid_project$"),
          keep=c("^information_aid:past_aid_project:good_type_bags$","^information_aid:past_aid_project:good_type_dictionary$", "^information_aid:past_aid_project:good_type_lamps$", "^information_aid:past_aid_project$", "^past_aid_project:good_type_bags$", "^past_aid_project:good_type_dictionary$", "^past_aid_project:good_type_lamps$", "^past_aid_project$"),
          covariate.labels = c(paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, "School Bags", sep="*\n"), 
                               paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, "Dictionaries", sep="*\n"), 
                               paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label , "Solar Lamps", sep="*\n"), 
                               paste(labels.all[labels.all$varname=="information_aid",]$label, labels.all[labels.all$varname=="past_aid_project",]$label, sep="*\n"), 
                               paste(labels.all[labels.all$varname=="past_aid_project",]$label, "School Bags", sep="*\n"),   
                               paste(labels.all[labels.all$varname=="past_aid_project",]$label, "Dictionaries", sep="*\n"),   
                               paste(labels.all[labels.all$varname=="past_aid_project",]$label, "Solar Lamps", sep="*\n"),   
                               
                               labels.all[labels.all$varname=="past_aid_project",]$label),
          omit="*",
          no.space=TRUE,
          label=paste("tab_good_heterogeneity", sep=""),
          table.placement = "H"
          
)

```

\FloatBarrier

\subsection{Figure A1 alternative measurement }
```{r, message=FALSE, warning=FALSE, results='asis', echo=FALSE}

this.survey=all.surveys
ha.results=data.frame(term=NA, estimate=NA, std.error=NA, robust.se=NA, statistic=NA, p.value=NA, conf.low=NA, conf.high=NA, model=NA, predictor=NA)

i=0
this.controls=gsub(" \\+ winner_votes","", controls)

h1 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_winner_votes + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
i=i+1
t=1
m=h1
ha.results[i,]$estimate = m$coefficients["z_winner_votes"] + m$coefficients["information_aid:z_winner_votes"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_winner_votes", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_winner_votes", "robust se"]^2 + 2*t*vcov(m)["z_winner_votes","information_aid:z_winner_votes"])
ha.results[i,]$term=labels.all[labels.all$varname=="winner_votes_imp",]$label
ha.results[i,]$p.value=coef(summary(m))["information_aid:z_winner_votes", "Pr(>|z|)"]
ha.results[i,]$model="Treatment Group"
i=i+1
t=0
ha.results[i,]$estimate = m$coefficients["z_winner_votes"] + m$coefficients["information_aid:z_winner_votes"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_winner_votes", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_winner_votes", "robust se"]^2 + 2*t*vcov(m)["z_winner_votes","information_aid:z_winner_votes"])
ha.results[i,]$term=labels.all[labels.all$varname=="winner_votes_imp",]$label
ha.results[i,]$model="Control Group"

h1b = clogit(as.formula(paste("incumbentselected ~ information_aid*z_winner_votes + information_aid*ps_total_votes + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
i=i+1
t=1
m=h1b
ha.results[i,]$estimate = m$coefficients["z_winner_votes"] + m$coefficients["information_aid:z_winner_votes"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_winner_votes", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_winner_votes", "robust se"]^2 + 2*t*vcov(m)["z_winner_votes","information_aid:z_winner_votes"])
ha.results[i,]$term=paste(labels.all[labels.all$varname=="winner_votes_imp",]$label, "(with Controls)")
ha.results[i,]$p.value=coef(summary(m))["information_aid:z_winner_votes", "Pr(>|z|)"]
ha.results[i,]$model="Treatment Group"
i=i+1
t=0
ha.results[i,]$estimate = m$coefficients["z_winner_votes"] + m$coefficients["information_aid:z_winner_votes"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_winner_votes", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_winner_votes", "robust se"]^2 + 2*t*vcov(m)["z_winner_votes","information_aid:z_winner_votes"])
ha.results[i,]$term=paste(labels.all[labels.all$varname=="winner_votes_imp",]$label, "(with Controls)")
ha.results[i,]$model="Control Group"


h2 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_ps_opposition_percent + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
i=i+1
t=1
m=h2
ha.results[i,]$estimate = m$coefficients["z_ps_opposition_percent"] + m$coefficients["information_aid:z_ps_opposition_percent"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_ps_opposition_percent", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_ps_opposition_percent", "robust se"]^2 + 2*t*vcov(m)["z_ps_opposition_percent","information_aid:z_ps_opposition_percent"])
ha.results[i,]$term="Opposition Votes at Poll. Station"
ha.results[i,]$p.value=coef(summary(m))["information_aid:z_ps_opposition_percent", "Pr(>|z|)"]
ha.results[i,]$model="Treatment Group"
i=i+1
t=0
ha.results[i,]$estimate = m$coefficients["z_ps_opposition_percent"] + m$coefficients["information_aid:z_ps_opposition_percent"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_ps_opposition_percent", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_ps_opposition_percent", "robust se"]^2 + 2*t*vcov(m)["z_ps_opposition_percent","information_aid:z_ps_opposition_percent"])
ha.results[i,]$term="Opposition Votes at Poll. Station"
ha.results[i,]$model="Control Group"


h3 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_log_winner_percent + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
i=i+1
t=1
m=h3
ha.results[i,]$estimate = m$coefficients["z_log_winner_percent"] + m$coefficients["information_aid:z_log_winner_percent"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_log_winner_percent", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_log_winner_percent", "robust se"]^2 + 2*t*vcov(m)["z_log_winner_percent","information_aid:z_log_winner_percent"])
ha.results[i,]$term=paste(labels.all[labels.all$varname=="winner_percent_imp",]$label, "(log)")
ha.results[i,]$p.value=coef(summary(m))["information_aid:z_log_winner_percent", "Pr(>|z|)"]
ha.results[i,]$model="Treatment Group"
i=i+1
t=0
ha.results[i,]$estimate = m$coefficients["z_log_winner_percent"] + m$coefficients["information_aid:z_log_winner_percent"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_log_winner_percent", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_log_winner_percent", "robust se"]^2 + 2*t*vcov(m)["z_log_winner_percent","information_aid:z_log_winner_percent"])
ha.results[i,]$term=paste(labels.all[labels.all$varname=="winner_percent_imp",]$label, "(log)")
ha.results[i,]$model="Control Group"



h4 = clogit(as.formula(paste("incumbentselected ~ information_aid*z_ps_victory_margin + information_aid*z_past_aid_project", this.controls, "+ strata(map_id) + cluster(resp_id)")), method="efron", this.survey)  
i=i+1
t=1
m=h4
ha.results[i,]$estimate = m$coefficients["z_ps_victory_margin"] + m$coefficients["information_aid:z_ps_victory_margin"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_ps_victory_margin", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_ps_victory_margin", "robust se"]^2 + 2*t*vcov(m)["z_ps_victory_margin","information_aid:z_ps_victory_margin"])
ha.results[i,]$term="Victory Margin at Polling Station"
ha.results[i,]$p.value=coef(summary(m))["information_aid:z_ps_victory_margin", "Pr(>|z|)"]
ha.results[i,]$model="Treatment Group"
i=i+1
t=0
ha.results[i,]$estimate = m$coefficients["z_ps_victory_margin"] + m$coefficients["information_aid:z_ps_victory_margin"]*t
ha.results[i,]$robust.se = sqrt(coef(summary(m))["z_ps_victory_margin", "robust se"]^2 + t^2*coef(summary(m))["information_aid:z_ps_victory_margin", "robust se"]^2 + 2*t*vcov(m)["z_ps_victory_margin","information_aid:z_ps_victory_margin"])
ha.results[i,]$term="Victory Margin at Polling Station"
ha.results[i,]$model="Control Group"



ha.results$conf.low=ha.results$estimate-qt(0.975,df=h1$n-1)*ha.results$robust.se
ha.results$conf.high=ha.results$estimate +qt(0.975,df=h1$n-1)*ha.results$robust.se


ha.results$exp_estimate = exp(ha.results$estimate)
ha.results=ha.results[!is.na(ha.results$model),]

ha.results=ha.results[order(ha.results$model, ha.results$model),]
ha.results=ha.results[!is.na(ha.results$estimate),]

pdf("./Output/interaction_plot_robust.pdf",width=6.5,height=7, family="Times")

dwplot(ha.results, 
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2),
       dot_args = list(aes(shape = model))
)+
  annotate("text", y = (1:nrow(ha.results[ha.results$model=="Treatment Group",])), x = rep(-0.25, nrow(ha.results[ha.results$model=="Treatment Group",])), label = paste("  p=", rev(round(signif(ha.results[ha.results$model=="Treatment Group",]$p.value,3),2))), size=3)+
  theme_bw() + xlab("Marginal Effect (Log Odds)") + ylab("") +
  theme(plot.title = element_text(face="bold"),
        legend.position = c(0.64, 0.01),
        legend.justification = c(0, 0), 
        legend.background = element_rect(colour="grey80"),
        legend.title = element_blank()) +
  scale_color_manual(values=wes_palette(n=4, name="Darjeeling1"))+
  scale_shape_discrete(name = "model")+
  coord_cartesian(xlim=c(-0.5, 1.0))


dev.off()
knitr::include_graphics("../Output/interaction_plot_robust.pdf")
cat(paste("\n", sep=""))

```
\FloatBarrier

\section{Supplementary Analysis}

\subsection{Table B1 all information treatments}
```{r, message=FALSE, warning=FALSE, results='asis', echo=FALSE}




h.all_info1 = clogit(as.formula(paste("incumbentselected ~  information_aid:past_aid_project +past_aid_project+                                      information_need:school_need_index + information_need:winner_percent_imp + school_need_index+
                                    information_votes:winner_percent_imp+information_votes:school_need_index +winner_percent_imp+family_children_attend_school:information_need+family_children_attend_school+
                                         strata(map_id) + 
                                      cluster(resp_id)")), method="efron", all.surveys)



h.all_info2 = clogit(as.formula(paste("incumbentselected ~  information_aid:number_aid_categories +number_aid_categories+                                      information_need:school_need_index + information_need:winner_percent_imp + school_need_index+
                                    information_votes:winner_percent_imp+information_votes:school_need_index +winner_percent_imp+ family_children_attend_school:information_need+family_children_attend_school+
                                         strata(map_id) + 
                                      cluster(resp_id)")), method="efron", all.surveys)

h.need_info = clogit(as.formula(paste("incumbentselected ~   winner_percent_imp:information_need + school_need_index:information_need + family_children_attend_school:information_need + school_need_index+winner_percent_imp+family_children_attend_school +
                                         strata(map_id) + 
                                        cluster(resp_id)")), method="efron", all.surveys)


h.voting_info = clogit(as.formula(paste("incumbentselected ~  information_votes:school_need_index + information_votes:winner_percent_imp +winner_percent_imp+school_need_index+
                                         strata(map_id) + 
                                      cluster(resp_id)")), method="efron", all.surveys)



stargazer(h.all_info1, h.all_info2, h.need_info, h.voting_info, type = "latex", 
          out=paste("./Output/tab_all_info_treatments_expanded.tex", sep=""), 
          column.labels = c("\\makecell{All \\\\Treatments}","\\makecell{All \\\\Treatments}", "\\makecell{Need \\\\Treatments}", "\\makecell{Voting \\\\Treatments}"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "Standard errors are clustered on incumbent. ",
          dep.var.caption  = "",
          title="All Information Treatments",
          order=c("^past_aid_project:information_aid$", "^past_aid_project$", 
                  "^number_aid_categories:information_aid$", "^number_aid_categories$",
                  "^school_need_index:information_need$",  "^winner_percent_imp:information_need$", "^school_need_index$","^family_children_attend_school:information_need$", "^family_children_attend_school$", "winner_percent_imp:information_votes", "school_need_index:information_votes", "winner_percent_imp"),
          keep=c("^past_aid_project:information_aid$", "^past_aid_project$", 
                  "^number_aid_categories:information_aid$", "^number_aid_categories$",
                  "^school_need_index:information_need$",  "^winner_percent_imp:information_need$", "^school_need_index$",  "^family_children_attend_school:information_need$", "^family_children_attend_school$", "winner_percent_imp:information_votes", "school_need_index:information_votes", "winner_percent_imp"),
          covariate.labels = c(labelme("past_aid_project:information_aid"), labelme("past_aid_project"),
                               labelme("number_aid_categories:information_aid"), labelme("number_aid_categories"),
                               labelme("school_need_index:information_need"),labelme("winner_percent_imp:information_need"),  labelme("school_need_index"),  labelme("family_children_attend_school:information_need"), labelme("family_children_attend_school"), labelme("winner_percent_imp:information_votes"),  labelme("school_need_index:information_votes"), labelme("winner_percent_imp")),
          omit="*",
          no.space=TRUE,
          label=paste("tab_all_info_treatments_expanded", sep=""),
          table.placement = "H"
)

```

\subsection{Table B2 interaction of aid information and transparency}
```{r aid transparency treatments interactions, message=FALSE, warning=FALSE, results='asis', echo=FALSE}

h.trans_donor = clogit(as.formula(paste("incumbentselected ~ past_aid_project + information_aid:past_aid_project +
                                 transparency_donor:past_aid_project + information_aid:transparency_donor:past_aid_project + 
                                 strata(map_id) + 
                                 cluster(resp_id)")), method="efron", all.surveys)


h.trans_radio = clogit(as.formula(paste("incumbentselected ~ past_aid_project + information_aid:past_aid_project +
                                        transparency_radio:past_aid_project + information_aid:transparency_radio:past_aid_project + 
                                        strata(map_id) + 
                                        cluster(resp_id)")), method="efron", all.surveys)


h.trans_all = clogit(as.formula(paste("incumbentselected ~ past_aid_project + information_aid:past_aid_project +
                                        transparency_radio:past_aid_project + information_aid:transparency_radio:past_aid_project + 
                                       transparency_donor:past_aid_project + information_aid:transparency_donor:past_aid_project + 
                                      transparency_donor:transparency_radio:past_aid_project + transparency_donor:transparency_radio:information_aid:past_aid_project 
                                        strata(map_id) + 
                                        cluster(resp_id)")), method="efron", all.surveys)

#summary(h.trans_all)

h.all_treat= clogit(as.formula(paste("incumbentselected ~ past_aid_project + information_aid:past_aid_project +
                                 information_need:past_aid_project + information_need:information_aid:past_aid_project +
                                     information_votes:past_aid_project + information_aid:information_votes:past_aid_project +
                                     + information_need:information_votes:past_aid_project +
                                     + information_aid:information_need:information_votes:past_aid_project +
                                      transparency_all:past_aid_project + information_aid:transparency_all:past_aid_project
                                    + information_need:transparency_all:past_aid_project
                                    + information_votes:transparency_all:past_aid_project
                                    + information_need:information_votes:transparency_all:past_aid_project
                                     strata(map_id) + 
                                     cluster(resp_id)")), method="efron", all.surveys)

#summary(h.all)


stargazer(h.trans_donor, h.trans_radio, h.trans_all, type = "latex", 
          out=paste("./Output/tab_aid_trans_interactions.tex", sep=""), 
          column.labels = c("\\makecell{Donor \\\\Treatment}","\\makecell{Radio \\\\Treatment}", "\\makecell{All \\\\Treatments}"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="Interaction of Aid Information Treatment with Transparency Treatments",
          order=c("^past_aid_project$","^past_aid_project:information_aid$","^past_aid_project:transparency_donor$", "^past_aid_project:information_aid:transparency_donor$", "^past_aid_project:transparency_radio$", "^past_aid_project:information_aid:transparency_radio$", "^past_aid_project:transparency_donor:transparency_radio$", "^past_aid_project:information_aid:transparency_donor:transparency_radio$"),
          keep=c("^past_aid_project$","^past_aid_project:information_aid$","^past_aid_project:transparency_donor$", "^past_aid_project:information_aid:transparency_donor$", "^past_aid_project:transparency_radio$", "^past_aid_project:information_aid:transparency_radio$", "^past_aid_project:transparency_donor:transparency_radio$", "^past_aid_project:information_aid:transparency_donor:transparency_radio$"),
          covariate.labels = c(labelme("past_aid_project"),labelme("past_aid_project:information_aid"),labelme("past_aid_project:transparency_donor"), labelme("past_aid_project:information_aid:transparency_donor"), labelme("past_aid_project:transparency_radio"), labelme("past_aid_project:information_aid:transparency_radio"), labelme("past_aid_project:transparency_donor:transparency_radio"), labelme("past_aid_project:information_aid:transparency_donor:transparency_radio")),
          omit="*",
          no.space=TRUE,
          label=paste("tab_aid_trans_interactions", sep=""),
          table.placement = "H"
)
```

\subsection{Table B2-B4 interaction with transparency treatments}
```{r, message=FALSE, warning=FALSE, results='asis', echo=FALSE}



h.trans_donor = clogit(as.formula(paste("incumbentselected ~ 
                                past_aid_project + 
                                school_need_index +
                                winner_percent +
                                 information_aid:past_aid_project +
                                 information_need:school_need_index+
                                 information_votes:winner_percent+
                                 transparency_donor:past_aid_project +
                                 transparency_donor:school_need_index +
                                 transparency_donor:winner_percent +
                                 information_aid:transparency_donor:past_aid_project + 
                                 information_need:transparency_donor:school_need_index + 
                                 information_votes:transparency_donor:winner_percent + 
                                 strata(map_id) + 
                                 cluster(resp_id)")), method="efron", all.surveys)


h.trans_radio = clogit(as.formula(paste("incumbentselected ~ 
                                past_aid_project + 
                                school_need_index +
                                winner_percent +
                                 information_aid:past_aid_project +
                                 information_need:school_need_index+
                                 information_votes:winner_percent+
                                 transparency_radio:past_aid_project +
                                 transparency_radio:school_need_index +
                                 transparency_radio:winner_percent +
                                 information_aid:transparency_radio:past_aid_project + 
                                 information_need:transparency_radio:school_need_index + 
                                 information_votes:transparency_radio:winner_percent + 
                                 strata(map_id) + 
                                 cluster(resp_id)")), method="efron", all.surveys)



h.trans_all = clogit(as.formula(paste("incumbentselected ~ 
                                past_aid_project + 
                                school_need_index +
                                winner_percent +
                                 information_aid:past_aid_project +
                                 information_need:school_need_index+
                                 information_votes:winner_percent+
                                 
                                 transparency_radio:past_aid_project +
                                 transparency_radio:school_need_index +
                                 transparency_radio:winner_percent +
                                 information_aid:transparency_radio:past_aid_project + 
                                 information_need:transparency_radio:school_need_index + 
                                 information_votes:transparency_radio:winner_percent + 
                                 transparency_donor:past_aid_project +
                                 transparency_donor:school_need_index +
                                 transparency_donor:winner_percent +
                                 information_aid:transparency_donor:past_aid_project + 
                                 information_need:transparency_donor:school_need_index + 
                                 information_votes:transparency_donor:winner_percent + 
                                 strata(map_id) + 
                                 cluster(resp_id)")), method="efron", all.surveys)




h.trans_donor_cat = clogit(as.formula(paste("incumbentselected ~ 
                                number_aid_categories + 
                                school_need_index +
                                winner_percent +
                                 information_aid:number_aid_categories +
                                 information_need:school_need_index+
                                 information_votes:winner_percent+
                                 transparency_donor:number_aid_categories +
                                 transparency_donor:school_need_index +
                                 transparency_donor:winner_percent +
                                 information_aid:transparency_donor:number_aid_categories + 
                                 information_need:transparency_donor:school_need_index + 
                                 information_votes:transparency_donor:winner_percent + 
                                 strata(map_id) + 
                                 cluster(resp_id)")), method="efron", all.surveys)


h.trans_radio_cat = clogit(as.formula(paste("incumbentselected ~ 
                                number_aid_categories + 
                                school_need_index +
                                winner_percent +
                                 information_aid:number_aid_categories +
                                 information_need:school_need_index+
                                 information_votes:winner_percent+
                                 transparency_radio:number_aid_categories +
                                 transparency_radio:school_need_index +
                                 transparency_radio:winner_percent +
                                 information_aid:transparency_radio:number_aid_categories + 
                                 information_need:transparency_radio:school_need_index + 
                                 information_votes:transparency_radio:winner_percent + 
                                 strata(map_id) + 
                                 cluster(resp_id)")), method="efron", all.surveys)



h.trans_all_cat = clogit(as.formula(paste("incumbentselected ~ 
                                number_aid_categories + 
                                school_need_index +
                                winner_percent +
                                 information_aid:number_aid_categories +
                                 information_need:school_need_index+
                                 information_votes:winner_percent+
                                 transparency_radio:number_aid_categories +
                                 transparency_radio:school_need_index +
                                 transparency_radio:winner_percent +
                                 information_aid:transparency_radio:number_aid_categories + 
                                 information_need:transparency_radio:school_need_index + 
                                 information_votes:transparency_radio:winner_percent + 
                                 transparency_donor:number_aid_categories +
                                 transparency_donor:school_need_index +
                                 transparency_donor:winner_percent +
                                 information_aid:transparency_donor:number_aid_categories + 
                                 information_need:transparency_donor:school_need_index + 
                                 information_votes:transparency_donor:winner_percent + 
                                 strata(map_id) + 
                                 cluster(resp_id)")), method="efron", all.surveys)




                           
  
  



stargazer(h.trans_donor, h.trans_radio, h.trans_all, type = "latex", 
          out=paste("./Output/tab_all_trans_interactions.tex", sep=""), 
          column.labels = c("\\makecell{Donor \\\\Treatment}","\\makecell{Radio \\\\Treatment}", "\\makecell{All \\\\Treatments}"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="Interaction of All Information Treatments with Transparency Treatments, Aid Project Count Coding",
          order=c(
            "^past_aid_project:information_aid:transparency_radio$", 
            "^school_need_index:information_need:transparency_radio$", 
            "^winner_percent:information_votes:transparency_radio$", 
            
            
            "^past_aid_project:information_aid:transparency_donor$", 
            "^school_need_index:information_need:transparency_donor$", 
            "^winner_percent:information_votes:transparency_donor$",
            
            "^past_aid_project:information_aid$",
            "^school_need_index:information_need$",
            "^winner_percent:information_votes$",
                  
                  "^past_aid_project:transparency_radio$",
                  "^school_need_index:transparency_radio$",
                  "^winner_percent:transparency_radio$",
                  

                  
                  "^past_aid_project:transparency_donor$",
                  "^school_need_index:transparency_donor$",
                  "^winner_percent:transparency_donor$",
           
                  
                  "^past_aid_project$", 
                  "^school_need_index$",
                  "^winner_percent$"
                  
                  ),
          
          keep=c(
            "^past_aid_project:information_aid:transparency_radio$", 
            "^school_need_index:information_need:transparency_radio$", 
            "^winner_percent:information_votes:transparency_radio$", 
            
            
            "^past_aid_project:information_aid:transparency_donor$", 
            "^school_need_index:information_need:transparency_donor$", 
            "^winner_percent:information_votes:transparency_donor$",
            
            "^past_aid_project:information_aid$",
            "^school_need_index:information_need$",
            "^winner_percent:information_votes$",
            
            "^past_aid_project:transparency_radio$",
            "^school_need_index:transparency_radio$",
            "^winner_percent:transparency_radio$",
            
            
            
            "^past_aid_project:transparency_donor$",
            "^school_need_index:transparency_donor$",
            "^winner_percent:transparency_donor$",
            
            
            "^past_aid_project$", 
            "^school_need_index$",
            "^winner_percent$"
            
          ),
          covariate.labels =    c(
            
            labelme("information_aid:transparency_radio:past_aid_project"), 
            labelme("information_need:transparency_radio:school_need_index"), 
            labelme("information_votes:transparency_radio:winner_percent_imp"), 
            
            labelme("information_aid:transparency_donor:past_aid_project"), 
            labelme("information_need:transparency_donor:school_need_index"), 
            labelme("information_votes:transparency_donor:winner_percent_imp"),
            
            labelme("information_aid:past_aid_project"),
            labelme("information_need:school_need_index"),
            labelme("information_votes:winner_percent_imp"),
            
            labelme("transparency_radio:past_aid_project"),
            labelme("transparency_radio:school_need_index"),
            labelme("transparency_radio:winner_percent_imp"),
            
            labelme("transparency_donor:past_aid_project"),
            labelme("transparency_donor:school_need_index"),
            labelme("transparency_donor:winner_percent_imp"),
            
            
            labelme("past_aid_project"), 
            labelme("school_need_index"),
            labelme("winner_percent_imp")
          ),
          omit="*",
          no.space=TRUE,
          label=paste("tab_all_trans_interactions", sep=""),
          table.placement = "H"
)




stargazer(h.trans_donor_cat, h.trans_radio_cat, h.trans_all_cat, type = "latex", 
          out=paste("./Output/tab_all_trans_interactions_cat.tex", sep=""), 
          column.labels = c("\\makecell{Donor \\\\Treatment}","\\makecell{Radio \\\\Treatment}", "\\makecell{All \\\\Treatments}"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="Interaction of All Information Treatments with Transparency Treatments, Aid Good Types Coding",
          order=c(
            "^number_aid_categories:information_aid:transparency_radio$", 
            "^school_need_index:information_need:transparency_radio$", 
            "^winner_percent:information_votes:transparency_radio$", 
            
            
            "^number_aid_categories:information_aid:transparency_donor$", 
            "^school_need_index:information_need:transparency_donor$", 
            "^winner_percent:information_votes:transparency_donor$",
            
            "^number_aid_categories:information_aid$",
            "^school_need_index:information_need$",
            "^winner_percent:information_votes$",
                  
                  "^number_aid_categories:transparency_radio$",
                  "^school_need_index:transparency_radio$",
                  "^winner_percent:transparency_radio$",
                  

                  
                  "^number_aid_categories:transparency_donor$",
                  "^school_need_index:transparency_donor$",
                  "^winner_percent:transparency_donor$",
           
                  
                  "^number_aid_categories$", 
                  "^school_need_index$",
                  "^winner_percent$"
                  
                  ),
          
          keep=c(
            "^number_aid_categories:information_aid:transparency_radio$", 
            "^school_need_index:information_need:transparency_radio$", 
            "^winner_percent:information_votes:transparency_radio$", 
            
            
            "^number_aid_categories:information_aid:transparency_donor$", 
            "^school_need_index:information_need:transparency_donor$", 
            "^winner_percent:information_votes:transparency_donor$",
            
            "^number_aid_categories:information_aid$",
            "^school_need_index:information_need$",
            "^winner_percent:information_votes$",
            
            "^number_aid_categories:transparency_radio$",
            "^school_need_index:transparency_radio$",
            "^winner_percent:transparency_radio$",
            
            
            
            "^number_aid_categories:transparency_donor$",
            "^school_need_index:transparency_donor$",
            "^winner_percent:transparency_donor$",
            
            
            "^number_aid_categories$", 
            "^school_need_index$",
            "^winner_percent$"
            
          ),
          covariate.labels =    c(
            
            labelme("information_aid:transparency_radio:number_aid_categories"), 
            labelme("information_need:transparency_radio:school_need_index"), 
            labelme("information_votes:transparency_radio:winner_percent_imp"), 
            
            labelme("information_aid:transparency_donor:number_aid_categories"), 
            labelme("information_need:transparency_donor:school_need_index"), 
            labelme("information_votes:transparency_donor:winner_percent_imp"),
            
            labelme("information_aid:number_aid_categories"),
            labelme("information_need:school_need_index"),
            labelme("information_votes:winner_percent_imp"),
            
            labelme("transparency_radio:number_aid_categories"),
            labelme("transparency_radio:school_need_index"),
            labelme("transparency_radio:winner_percent_imp"),
            
            labelme("transparency_donor:number_aid_categories"),
            labelme("transparency_donor:school_need_index"),
            labelme("transparency_donor:winner_percent_imp"),
            
            
            labelme("number_aid_categories"), 
            labelme("school_need_index"),
            labelme("winner_percent_imp")
          ),
          omit="*",
          no.space=TRUE,
          label=paste("tab_all_trans_interactions_cat", sep=""),
          table.placement = "H"
)


```


\section{Summary Statistics}
\subsection{Table C1 Local Councillor Sample Statistics}
```{r, message=FALSE, warning=FALSE, results='asis', echo=F}

#for summary stats we have to reload the labels so we're not just working with alternative specific variables
labels=read.csv("./data/labels.csv",stringsAsFactors=FALSE)
labels=labels[labels$include==1,]
labels=subset(labels, !is.na(varname))
labels = labels[order(labels$label),]


summarytable=data.frame(Variable=NA, In_Sample=NA,  Out_of_Sample=NA, Difference=NA)
schools.lc$constituencyid=as.integer(factor(schools.lc$constituencyid))
schools.lc$ps_ward_id=as.integer(factor(schools.lc$constituencyid))
schools.mp$constituencyid=as.integer(factor(schools.lc$constituencyid))
schools.mp$ps_ward_id=as.integer(factor(schools.lc$constituencyid))

schools.lc=select_if(schools.lc, is.numeric)

schools.mp=select_if(schools.mp, is.numeric)



schools.lc.mean=aggregate(schools.lc, list(c(schools.lc$ps_ward_id)), mean)
schools.lc.sum=aggregate(schools.lc, list(c(schools.lc$ps_ward_id)), sum)

schools.lc.sum$insample=ifelse(schools.lc.sum$insample>0,1,schools.lc.sum$insample)

schools.mp.mean=aggregate(schools.mp, list(c(schools.mp$constituencyid)), mean)
schools.mp.sum=aggregate(schools.mp, list(c(schools.mp$constituencyid)), sum)
schools.mp.sum$insample=ifelse(schools.mp.sum$insample>0,1,schools.mp.sum$insample)


#LC Sample
varlist=data.frame(
  var_name=c("school_enrollment_total", "school_num_teachers", "student_to_teacher_ratio", "past_aid_project", "num_schools_in_ward_tot", "total_turnout", "total_vm", "total_regvoters", "total_percent", "DPP", "UDF", "MCP", "PP", "Independent", "pop_per_hectacre"), 
  label=c("Mean School Enrollment", "Mean Number of Teachers", "Mean Student to Teacher Ratio", "Number of Aid Projects", "Number of Schools", "Turnout", "Incumbent Victory Margin", "Registered Voters", "Incumbent Percent", "DPP Incumbent", "UDF Incumbent", "MCP Incumbent", "PP Incumbent", "Independent Incumbent", "Average School Population Density"), 
  df=c("schools.lc.mean", "schools.lc.mean", "schools.lc.mean", "schools.lc.sum", "schools.lc.mean", "schools.lc.mean", "schools.lc.mean", "schools.lc.mean", "schools.lc.mean","schools.lc.mean","schools.lc.mean","schools.lc.mean","schools.lc.mean","schools.lc.mean", "schools.lc.mean"),
  stringsAsFactors = FALSE)

#varlist=varlist[3,]
j=0

for(var in varlist$var_name){
  j=j+1
  eval(parse(text=paste("this.df=",varlist[j,]$df, sep="")))

  eval(parse(text=paste("this.df$this.var=", varlist[j,]$df,"$", var, sep="")))
  
  #this.df$this.var=schools.lc.sum$total_votes_local
  
  N=nrow(this.df)
  N.insample=nrow(this.df[this.df$insample==1,])
  N.outsample=nrow(this.df[this.df$insample==0,])
  m.insample = mean(this.df[!is.na(this.df$this.var) & this.df$insample==1,]$this.var)
  m.outsample = mean(this.df[!is.na(this.df$this.var) & this.df$insample==0,]$this.var)
  s.insample = sd(this.df[!is.na(this.df$this.var) & this.df$insample==1,]$this.var)
  s.outsample = sd(this.df[!is.na(this.df$this.var) & this.df$insample==0,]$this.var)
  m.diff=m.insample-m.outsample
  s.diff=sqrt((s.outsample^2/N.outsample)+(s.insample^2/N.outsample))   
  
  summarytable[j,]$Variable=varlist[j,]$label
  summarytable[j,]$In_Sample=paste(round(m.insample,3)," (", round(s.insample,3), ")", sep="")
  summarytable[j,]$Out_of_Sample=paste(round(m.outsample,3)," (", round(s.outsample,3), ")", sep="")
  summarytable[j,]$Difference=paste(round(m.diff,3)," (", round(s.diff,3), ")", sep="")

}
print(xtable(summarytable,  label="tab_sample_lc", caption="Local Councillor Sample Statistics"), include.rownames=FALSE,  caption.placement = "top", file=paste("output/tab_sample_lc.tex", sep=""), hline.after=c(-1, 0), tabular.environment = "longtable")
print(xtable(summarytable,  label="tab_sample_lc", caption="Local Councillor Sample Statistics"), include.rownames=FALSE,  caption.placement = "top", hline.after=c(-1, 0), tabular.environment = "longtable")
   
```

\subsection{Table C2 MP Sample Statistics}
```{r, message=FALSE, warning=FALSE, results='asis', echo=F}


#MP Sample
varlist=data.frame(
  var_name=c("school_enrollment_total", "school_num_teachers", "student_to_teacher_ratio", "past_aid_project", "num_schools_in_constituency_tot", "total_turnout", "total_vm", "total_regvoters", "total_percent", "DPP", "UDF", "MCP", "PP", "Independent", "pop_per_hectacre"), 
  label=c("Mean School Enrollment", "Mean Number of Teachers", "Mean Student to Teacher Ratio", "Number of Aid Projects", "Number of Schools", "Turnout", "Incumbent Victory Margin", "Registered Voters", "Incumbent Percent", "DPP Incumbent", "UDF Incumbent", "MCP Incumbent", "PP Incumbent", "Independent Incumbent", "Average School Population Density"), 
  df=c("schools.mp.mean", "schools.mp.mean", "schools.mp.mean", "schools.mp.sum", "schools.mp.mean", "schools.mp.mean", "schools.mp.mean", "schools.mp.mean", "schools.mp.mean","schools.mp.mean","schools.mp.mean","schools.mp.mean","schools.mp.mean","schools.mp.mean", "schools.mp.mean"),
  stringsAsFactors = FALSE)


j=0
for(var in varlist$var_name){
  j=j+1
  eval(parse(text=paste("this.df=",varlist[j,]$df, sep="")))

  eval(parse(text=paste("this.df$this.var=", varlist[j,]$df,"$", var, sep="")))
  
  #this.df$this.var=schools.lc.sum$total_votes_local
  
  N=nrow(this.df)
  N.insample=nrow(this.df[this.df$insample==1,])
  N.outsample=nrow(this.df[this.df$insample==0,])
  m.insample = mean(this.df[!is.na(this.df$this.var) & this.df$insample==1,]$this.var)
  m.outsample = mean(this.df[!is.na(this.df$this.var) & this.df$insample==0,]$this.var)
  s.insample = sd(this.df[!is.na(this.df$this.var) & this.df$insample==1,]$this.var)
  s.outsample = sd(this.df[!is.na(this.df$this.var) & this.df$insample==0,]$this.var)
  m.diff=m.insample-m.outsample
  s.diff=sqrt((s.outsample^2/N.outsample)+(s.insample^2/N.outsample))   
  
  summarytable[j,]$Variable=varlist[j,]$label
  summarytable[j,]$In_Sample=paste(round(m.insample,3)," (", round(s.insample,3), ")", sep="")
  summarytable[j,]$Out_of_Sample=paste(round(m.outsample,3)," (", round(s.outsample,3), ")", sep="")
  summarytable[j,]$Difference=paste(round(m.diff,3)," (", round(s.diff,3), ")", sep="")

}
print(xtable(summarytable,  label="tab_sample_mp", caption="MP Sample Statistics"), include.rownames=FALSE,  caption.placement = "top", file=paste("output/tab_sample_mp.tex", sep=""), hline.after=c(-1, 0), tabular.environment = "longtable")
print(xtable(summarytable,  label="tab_sample_mp", caption="MP Sample Statistics"), include.rownames=FALSE,  caption.placement = "top", hline.after=c(-1, 0), tabular.environment = "longtable")

```


\subsection{Table C3-C4 Summary Statistics}
```{r, message=FALSE, warning=FALSE, results='asis', echo=F}



#Summary Statistics
summarytable=data.frame(Variable=NA, Mean=NA, SD=NA,  Details=NA)
for(this.dfname in c("mp.survey", "c.survey")){
  j=0
  model="1"
  for(i in 1:nrow(labels.all)){
    this.df=eval(parse(text=this.dfname))

    eval(parse(text=paste("this.df$tempvar=this.df$", labels.all[i,]$varname, sep="")))

   m=mean(this.df[!is.na(this.df$tempvar),]$tempvar)
   sd=sd(this.df[!is.na(this.df$tempvar) ,]$tempvar)

    
    if(!is.na(m)){
      j=j+1
      summarytable[j,]$Variable=labels.all[i,]$label
      summarytable[j,]$Mean=paste(round(m,3))
      summarytable[j,]$SD=paste(round(sd,3))
      summarytable[j,]$Details=labels.all[i,]$label_description
    }
    
  }

  print(xtable(summarytable,  label=paste("tab_summary_", this.dfname, sep=""), caption="Summary Statistics"), include.rownames=FALSE,  caption.placement = "top", file=paste("output/summary_", this.dfname, ".tex", sep=""), hline.after=c(-1, 0), tabular.environment = "longtable")
   
  print(xtable(summarytable,  label=paste("tab_summary_", this.dfname, sep=""), caption="Summary Statistics"), include.rownames=FALSE,  caption.placement = "top", hline.after=c(-1, 0), tabular.environment = "longtable")
  
}



```


\subsection{Figure C1 Knowledge Statistics}

```{r knowledge stats, message=FALSE, warning=FALSE, results='asis', echo=F}


c.byresp <- c.survey[!duplicated(c.survey[,c("ps_ward_id")]),]
#unique(c.survey$ps_ward_id)
mp.byresp <- mp.survey[!duplicated(mp.survey[,c("constituencyid")]),]


#create dataframe to plot summary data
c.df=data.frame(varmean=NA, group=NA, sd=NA, upper=NA, lower=NA)
c.df[1,]$varmean=mean(c.byresp$test_least_classes, na.rm=TRUE)*100
c.df[1,]$sd=sd(c.byresp$test_least_classes, na.rm=TRUE)*100
c.df[1,]$upper=c.df[1,]$varmean + 1.96*c.df[1,]$sd
c.df[1,]$lower=c.df[1,]$varmean - 1.96*c.df[1,]$sd
c.df[1,]$group=" Q1: School with \nLeast Classrooms"
c.df[2,]$varmean=mean(c.byresp$test_most_enrollment, na.rm=TRUE)*100
c.df[2,]$sd=sd(c.byresp$test_most_enrollment, na.rm=TRUE)*100
c.df[2,]$upper=c.df[2,]$varmean + 1.96*c.df[2,]$sd
c.df[2,]$lower=c.df[2,]$varmean - 1.96*c.df[2,]$sd
c.df[2,]$group=" Q2: School with \nMost Students"
c.df[3,]$varmean=mean(c.byresp$test_enrollment_specific, na.rm=TRUE)*100
c.df[3,]$sd=sd(c.byresp$test_enrollment_specific, na.rm=TRUE)*100
c.df[3,]$upper=c.df[3,]$varmean + 1.96*c.df[3,]$sd
c.df[3,]$lower=c.df[3,]$varmean - 1.96*c.df[3,]$sd
c.df[3,]$group=" Q3: Number Enrolled"
c.df[4,]$varmean=mean(c.byresp$test_least_percentvotes, na.rm=TRUE)*100
c.df[4,]$sd=sd(c.byresp$test_least_percentvotes, na.rm=TRUE)*100
c.df[4,]$upper=c.df[3,]$varmean + 1.96*c.df[3,]$sd
c.df[4,]$lower=c.df[3,]$varmean - 1.96*c.df[3,]$sd
c.df[4,]$group=" Q4: School with Lowest\n Percent Votes"
c.df[5,]$varmean=mean(c.byresp$test_percentvotes_specific, na.rm=TRUE)*100
c.df[5,]$sd=sd(c.byresp$test_percentvotes_specific, na.rm=TRUE)*100
c.df[5,]$upper=c.df[3,]$varmean + 1.96*c.df[3,]$sd
c.df[5,]$lower=c.df[3,]$varmean - 1.96*c.df[3,]$sd
c.df[5,]$group=" Q5: Percent Votes"
c.df[6,]$varmean=mean(c.byresp$test_most_projects, na.rm=TRUE)*100
c.df[6,]$sd=sd(c.byresp$test_most_projects, na.rm=TRUE)*100
c.df[6,]$upper=c.df[3,]$varmean + 1.96*c.df[3,]$sd
c.df[6,]$lower=c.df[3,]$varmean - 1.96*c.df[3,]$sd
c.df[6,]$group=" Q6: School with Most\n Donor Projects"
c.df[7,]$varmean=mean(c.byresp$test_donor_specific, na.rm=TRUE)*100
c.df[7,]$sd=sd(c.byresp$test_donor_specific, na.rm=TRUE)*100
c.df[7,]$upper=c.df[3,]$varmean + 1.96*c.df[3,]$sd
c.df[7,]$lower=c.df[3,]$varmean - 1.96*c.df[3,]$sd
c.df[7,]$group=" Q7: Name(s) of Donor(s)"


c.df$group2=factor(c.df$group, 
                 levels=names(sort(table(c.df$group), 
                                   decreasing=FALSE)))


#create dataframe to plot summary data
mp.df=data.frame(varmean=NA, group=NA, sd=NA, upper=NA, lower=NA)
mp.df[1,]$varmean=mean(mp.byresp$test_least_classes, na.rm=TRUE)*100
mp.df[1,]$sd=sd(mp.byresp$test_least_classes, na.rm=TRUE)*100
mp.df[1,]$upper=mp.df[1,]$varmean + 1.96*mp.df[1,]$sd
mp.df[1,]$lower=mp.df[1,]$varmean - 1.96*mp.df[1,]$sd
mp.df[1,]$group=" Q1: School with \nLeast Classrooms"
mp.df[2,]$varmean=mean(mp.byresp$test_most_enrollment, na.rm=TRUE)*100
mp.df[2,]$sd=sd(mp.byresp$test_most_enrollment, na.rm=TRUE)*100
mp.df[2,]$upper=mp.df[2,]$varmean + 1.96*mp.df[2,]$sd
mp.df[2,]$lower=mp.df[2,]$varmean - 1.96*mp.df[2,]$sd
mp.df[2,]$group=" Q2: School with \nMost Students"
mp.df[3,]$varmean=mean(mp.byresp$test_enrollment_specific, na.rm=TRUE)*100
mp.df[3,]$sd=sd(mp.byresp$test_enrollment_specific, na.rm=TRUE)*100
mp.df[3,]$upper=mp.df[3,]$varmean + 1.96*mp.df[3,]$sd
mp.df[3,]$lower=mp.df[3,]$varmean - 1.96*mp.df[3,]$sd
mp.df[3,]$group=" Q3: Number Enrolled"
mp.df[4,]$varmean=mean(mp.byresp$test_least_percentvotes, na.rm=TRUE)*100
mp.df[4,]$sd=sd(mp.byresp$test_least_percentvotes, na.rm=TRUE)*100
mp.df[4,]$upper=mp.df[3,]$varmean + 1.96*mp.df[3,]$sd
mp.df[4,]$lower=mp.df[3,]$varmean - 1.96*mp.df[3,]$sd
mp.df[4,]$group=" Q4: School with Lowest\n Percent Votes"
mp.df[5,]$varmean=mean(mp.byresp$test_percentvotes_specific, na.rm=TRUE)*100
mp.df[5,]$sd=sd(mp.byresp$test_percentvotes_specific, na.rm=TRUE)*100
mp.df[5,]$upper=mp.df[3,]$varmean + 1.96*mp.df[3,]$sd
mp.df[5,]$lower=mp.df[3,]$varmean - 1.96*mp.df[3,]$sd
mp.df[5,]$group=" Q5: Percent Votes"
mp.df[6,]$varmean=mean(mp.byresp$test_most_projects, na.rm=TRUE)*100
mp.df[6,]$sd=sd(mp.byresp$test_most_projects, na.rm=TRUE)*100
mp.df[6,]$upper=mp.df[3,]$varmean + 1.96*mp.df[3,]$sd
mp.df[6,]$lower=mp.df[3,]$varmean - 1.96*mp.df[3,]$sd
mp.df[6,]$group=" Q6: School with Most\n Donor Projects"
mp.df[7,]$varmean=mean(mp.byresp$test_donor_specific, na.rm=TRUE)*100
mp.df[7,]$sd=sd(mp.byresp$test_donor_specific, na.rm=TRUE)*100
mp.df[7,]$upper=mp.df[3,]$varmean + 1.96*mp.df[3,]$sd
mp.df[7,]$lower=mp.df[3,]$varmean - 1.96*mp.df[3,]$sd
mp.df[7,]$group=" Q7: Name(s) of Donor(s)"

mp.df$group2=factor(mp.df$group, 
                   levels=names(sort(table(mp.df$group), 
                                     decreasing=FALSE)))

pdf("./Output/c_priorplot.pdf",width=6.5,height=7, family="Times")


ggplot(c.df, aes(y=varmean, x=group)) + 
  geom_bar(position=position_dodge(), stat="identity", fill="blue", width=0.5) +
  
  
  theme_bw() +
  theme(
    axis.title.x = element_text(family="Times", face="bold", size=25),
    axis.title.y = element_text(family="Times", face="bold", size=25),
    axis.text.x  = element_text(family="Times", face="bold", size=15),
    axis.text.y  = element_text(family="Times", face="bold", size=15),
    text=element_text(family="Times"), 
  )+
  labs(y = "% Correct", x="")+
  expand_limits(y=c(0,85))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
ggtitle("")
dev.off()

pdf("./Output/mp_priorplot.pdf",width=6.5,height=7, family="Times")

ggplot(mp.df, aes(y=varmean, x=group)) + 
  geom_bar(position=position_dodge(), stat="identity", fill="blue", width=0.5) +
  
  
  theme_bw() +
  theme(
    axis.title.x = element_text(family="Times", face="bold", size=25),
    axis.title.y = element_text(family="Times", face="bold", size=25),
    axis.text.x  = element_text(family="Times", face="bold", size=15),
    axis.text.y  = element_text(family="Times", face="bold", size=15),
    text=element_text(family="Times"), 
  )+
  labs(y = "% Correct", x="")+
  ggtitle("")+
  
  expand_limits(y=c(0,85))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
dev.off()

knitr::include_graphics("../Output/c_priorplot.pdf")

```
\subsection{Figure C2 Knowledge Statistics}

```{r, echo=F}
knitr::include_graphics("../Output/c_priorplot.pdf")

```


\subsection{Table C5-C7 Attrition Statistics }
```{r, message=FALSE, warning=FALSE, results='asis', echo=F}



#effect of treatment on attritoin

c.survey.att=aggregate(c.survey.all$attritted, by=list(c.survey.all$map_id), max)
c.survey.treat=aggregate(c.survey.all$information_aid, by=list(c.survey.all$map_id), max)
c.survey.all$any_aid_treat = c.survey.treat[match(c.survey.all$map_id, c.survey.treat$Group.1),"x"]
c.survey.all$att_any = c.survey.att[match(c.survey.all$map_id, c.survey.att$Group.1),"x"]

mp.survey.att=aggregate(mp.survey.all$attritted, by=list(mp.survey.all$map_id), max)
mp.survey.treat=aggregate(mp.survey.all$information_aid, by=list(mp.survey.all$map_id), max)
mp.survey.all$any_aid_treat = mp.survey.treat[match(mp.survey.all$map_id, mp.survey.treat$Group.1),"x"]
mp.survey.all$att_any = mp.survey.att[match(mp.survey.all$map_id, mp.survey.att$Group.1),"x"]

lm_mp_att=(lm(att_any ~information_aid, data=mp.survey.all))
lm_lc_att=(lm(att_any~information_aid, data=c.survey.all))


stargazer(lm_mp_att, lm_lc_att, type = "latex", 
          out=paste("./output/attrition_treat_regression.tex", sep=""), 
          column.labels = c( "MP Survey", "Councillor Survey"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "Attrition",
          title="The Effect of Treatment on Survey Attrition",
          covariate.labels=c("Treatment", "Intercept"), no.space=TRUE,
          label=paste("tab_attrition_treat_regression", sep=""),
          table.placement = "H"
)


#Attrition summary statistics
summarytable=data.frame(Variable=NA, NotAttritted=NA, Attritted=NA,  Difference=NA)



for(this.dfname in c("c.survey.all", "mp.survey.all")){
  j=0
  model="1"
  for(i in 1:nrow(labels)){
    if(grepl("*knowledge*", labels[i,]$varname)){next}
    if(grepl("*understand*", labels[i,]$varname)){next}
    #i=2
    this.df=eval(parse(text=this.dfname))
    if(this.dfname=="mp.survey.all"){this.df$id=this.df$constituencyid}
    if(this.dfname=="c.survey.all"){this.df$id=this.df$ps_ward_id}
    
  
    
      
    eval(parse(text=paste("this.df$tempvar=this.df$", labels[i,]$varname, sep="")))

    this.df.unique=aggregate(this.df$attritted, by=list(this.df$id), max)
    this.df.unique$id= this.df.unique$Group.1
    this.df.unique$attritted= this.df.unique$x
    
    N=nrow(this.df.unique)
    N.att0=nrow(this.df.unique[this.df.unique$attritted==0,])
    N.att1=nrow(this.df.unique[this.df.unique$attritted==1,])
    
    m.att0=mean(this.df[!is.na(this.df$tempvar) & this.df$attritted==0,]$tempvar)
    m.att1=mean(this.df[!is.na(this.df$tempvar) & this.df$attritted==1,]$tempvar)
    
    sd.att0=sd(this.df[!is.na(this.df$tempvar) & this.df$attritted==0,]$tempvar)
    sd.att1=sd(this.df[!is.na(this.df$tempvar) & this.df$attritted==1,]$tempvar)
    
    m.diff=m.att1-m.att0
    sd.diff=sqrt((sd.att0^2/N.att0)+(sd.att1^2/N.att1))
    
    if(!is.na(m.att1) & N.att1>0){
      j=j+1
      summarytable[j,]$Variable=labels[i,]$label
      summarytable[j,]$NotAttritted=paste(round(m.att0,3)," (", round(sd.att0,3), ")", sep="")
      summarytable[j,]$Attritted=paste(round(m.att1,3)," (", round(sd.att1,3), ")", sep="")
      summarytable[j,]$Difference=paste(round(m.diff,3)," (", round(sd.diff,3), ")", sep="")
      ag=aggregate(this.df$tempvar, by=list(this.df$id), FUN=mean, na.rm=TRUE)
      assign(paste("m_", labels[i,]$varname, sep=""), ag$x)
      model=paste(model, paste("m_", labels[i,]$varname, sep=""), sep=" + ")
    }
    
  }
  m_attritted=this.df.unique$attritted
  m=lm(as.formula(paste("m_attritted", model, sep="~")))
  summary(m)

  assign(paste("m_", this.dfname, sep=""), m)

print(xtable(summarytable,  label=paste("tab_attrition_", this.dfname, sep=""), caption="Summary Statistics by Survey Attrition Status"), include.rownames=FALSE, file=paste("./output/attrition_summary_", this.dfname, ".tex", sep=""), caption.placement = "top", hline.after=c(-1, 0), tabular.environment = "longtable")

print(xtable(summarytable,  label=paste("tab_attrition_", this.dfname, sep=""), caption="Summary Statistics by Survey Attrition Status"), include.rownames=FALSE, caption.placement = "top", hline.after=c(-1, 0), tabular.environment = "longtable")
}

#note stargazer does not support long tables
stargazer(m_mp.survey.all, m_c.survey.all, type = "latex", 
          out=paste("./output/attrition_regression.tex", sep=""), 
          column.labels = c( "MP Survey", "Councillor Survey"),
          model.names=FALSE, keep.stat = c("n","f", "rsq"), dep.var.labels.include=FALSE,
          notes = "",
          dep.var.caption  = "",
          title="The Effect of Covariates on Survey Attrition",
          covariate.labels=summarytable$Variable, no.space=TRUE,
          label=paste("tab_attrition_regression", sep=""),
          table.placement = "H"
)

```



\section{Figure C3 Correlation Matrix}
```{r tab_correlation_matrix, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}

varlist=c("school_need_index", "past_aid_project", "winner_percent_imp", "family_children_attend_school", "number_aid_categories", "pop_density", "temporary_to_permanent_ratio", "student_to_classroom_ratio", "student_to_teacher_ratio", "log_number_of_students")

labellist=unlist(lapply(varlist, function(x) labelme(x)))
                
                
this.df=subset(all.surveys, select=c(varlist))

var.cor=cor(this.df, use="complete.obs")

row.names(var.cor)=labellist
colnames(var.cor)=labellist


##  different color series
col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F",
                           "cyan", "#007FFF", "blue", "#00007F"))
pdf("./Output/correlationplot.pdf",width=7,height=6, family="Times")
corrplot(var.cor, type = "upper", order = "hclust", method="circle",
         tl.col = "black", tl.srt = 45, tl.cex=0.7, cl.lim=c(-1,1), col=col4(50))
dev.off()

corrplot(var.cor, type = "upper", order = "hclust", method="circle",
         tl.col = "black", tl.srt = 45, tl.cex=0.7, cl.lim=c(-1,1), col=col4(50))


```
